<?php
/*
  Template Name: single post template
 */
?>
<?php
get_header();
?>
<div class="container clearfix">
    <!-- Start carousel-demo5 -->
    <div id="carousel-demo5" class="sliderkit">
        <div class="sliderkit-nav">
            <div class="sliderkit-nav-clip">
                <ul>
                    <?php
                    the_post();
                    //$postCats = get_the_category();
                    //var_dump($postCats);exit;
                    $vidCatId = get_cat_ID('videos');
                    if (in_category($vidCatId)) {
                        $isVideo = TRUE;
                    } else {
                        $isVideo = FALSE;
                    }
                    $curID = get_the_ID();
                    $newsCatId = get_cat_ID('news');
                    $postCategory = getColorClassName(get_the_ID());
                    $headerPosts = get_posts(array('category__and' => array($postCategory->term_id, $newsCatId), 'numberposts' => 8));
                    foreach ($headerPosts as $headerPost) {
                        if ($curID === $headerPost->ID) {
                            continue;
                        }
                        ?>
                        <li>
                            <a href="<?php echo get_permalink($headerPost->ID); ?>" title=""><?php echo get_the_post_thumbnail($headerPost->ID); ?></a>
                            <h3><a href="<?php echo get_permalink($headerPost->ID); ?>"><?php echo get_the_title($headerPost->ID); ?></a></h3>
                        </li>
                    <?php }
                    ?>
                </ul>
            </div>
            <div class="sliderkit-btn sliderkit-nav-btn sliderkit-nav-prev"><a href="#" title="Scroll to the left"><span>Previous</span></a></div>
            <div class="sliderkit-btn sliderkit-nav-btn sliderkit-nav-next"><a href="#" title="Scroll to the right"><span>Next</span></a></div>
        </div>
    </div>
    <!-- // end of carousel-demo5 -->
    <div class="rightCol">

        <div class="inner">

            <h1><?php the_title(); ?></h1>
            <div class="socialShare">
                <div class="rightSocialShare clearfix">
                    <div class="oneSocial">
                        <div class="fb-like" data-href="https://www.facebook.com/Tahrir.News.Official" data-layout="box_count" data-action="like" data-show-faces="false" data-share="false"></div>
                    </div><!-- oneSocial -->
                    <div class="oneSocial">
                        <div id="facebook" class="det_fbShare" data-url="<?php the_permalink(); ?>" data-text="put any text here" data-title="&nbsp;"></div>
                    </div><!-- oneSocial -->
                    <div class="oneSocial">
                        <div id="twitter" class="det_tweet" data-url="<?php the_permalink(); ?>" data-text="put any text here" data-title="&nbsp;"></div>
                    </div><!-- oneSocial -->
                    <div class="oneSocial">
                        <div class="g-plus" data-action="share" data-annotation="vertical-bubble" data-height="60" data-href="<?php the_permalink(); ?>"></div>
                    </div><!-- oneSocial -->
                </div>

            </div><!-- socialShare -->
            <?php
            $thePostAlbum = loadAlbum(get_the_ID());
            $thumb = getThumbForAlbum(get_the_ID());
            if ($thumb) {
                array_unshift($thePostAlbum, $thumb);
            }
            if ($thePostAlbum) {
                ?>
                <div class="photoAlbums">
                    <div class="mygallery">
                        <div class="tn3 album">
                            <h4>Fixed Dimensions</h4>
                            <div class="tn3 description">Images with fixed dimensions</div>
                            <div class="tn3 thumb">images/35x35/1.jpg</div>
                            <ol>
                                <?php foreach ($thePostAlbum as $image) { ?>
                                    <li>
                                        <h4><?php echo $image['caption'] ?></h4>
                                        <div class="tn3 description"></div>
                                        <a href="<?php echo $image['src'] ?>">
                                            <img src="<?php echo $image['src'] ?>" alt="" />
                                        </a>
                                    </li>
                                <?php }
                                ?>
                            </ol>
                        </div>
                    </div>
                </div><!-- photoAlbums -->
            <?php }
            ?>

            <div class="homeadvsLeaderBoard"><img src="<?php bloginfo('template_url') ?>/images/leaderBoard.jpg" alt="" /></div>

            <div class="theNewContent">
                <?php
                if (!$isVideo) {
                    echo strip_shortcodes(get_the_content());
                } else {
                    ?>
                    <iframe width="465" height="235"
                            src="<?php echo getVideoEmbedURL(get_the_content()); ?>">
                    </iframe>
                <?php }
                ?>
            </div><!-- theNewContent -->

            <div id="comment">
                <span class="commentTitle"> التعليقات </span>                        
                <div class="fb-comments" data-href="<?php the_permalink(); ?>" data-width="730" data-numposts="5"></div>
            </div><!-- comment -->
            <?php
            $postTags = get_the_tags();
            $hasTags = !empty($postTags);
            if ($postTags) {
                $tagIdsArr = array();
                foreach ($postTags as $tag) {
                    $tagIdsArr[] = $tag->term_id;
                }
            }
            ?>
            <div class="moreNews">
                <h2> سيعجبك أيضا </h2>
                <div class="rightMN">
                    <?php
                    $relatedPosts = array();
                    if ($hasTags) {
                        $relatedPostsAnd = get_posts(array('category' => $newsCatId, 'number_posts' => 6, 'tag__and' => $tagIdsArr));
                        if (count($relatedPostsAnd) < 6) {
                            $numPosts = 6 - count($relatedPostsAnd);
                            $relatedPostsOr = get_posts(array('category' => $newsCatId, 'number_posts' => $numPosts, 'tag__in' => $tagIdsArr));
                            $relatedPostsAndOr = array_unique(array_merge($relatedPostsAnd, $relatedPostsOr), SORT_REGULAR);
                            if (count($relatedPostsAndOr) < 6) {
                                $numPosts = 6 - count($relatedPostsAndOr);
                                $relatedPostsCat = get_posts(array('category__and' => array($postCategory->term_id, $newsCatId), 'numberposts' => $numPosts));
                                $relatedPosts = array_unique(array_merge($relatedPostsAndOr, $relatedPostsCat), SORT_REGULAR);
                            } else {
                                $relatedPosts = $relatedPostsAndOr;
                            }
                        } else {
                            $relatedPosts = $relatedPostsAnd;
                        }
                    } else {
                        $relatedPosts = get_posts(array('category__and' => array($postCategory->term_id, $newsCatId), 'numberposts' => 5));
                    }
                    if ($relatedPosts) {
                        foreach ($relatedPosts as $relatedPost) {
                            if ($relatedPost->ID == $curID) {
                                continue;
                            }
                            $meta = get_post_meta($relatedPost->ID);
                            ?>
                            <div class="rightMNBox">
                                <span class="imageMN"><?php echo get_the_post_thumbnail($relatedPost->ID, 'thumbnail'); ?></span>
                                <h3><a href="<?php echo get_permalink($relatedPost->ID); ?>"><?php echo get_the_title($relatedPost->ID) ?></a></h3>
                                <div class="MNBottom">
                                    <span class="f-r">
                                        <span class="postTimeIcon"></span>
                                        <span class="postTimeIn"><?php echo date('n/j/Y', strtotime($relatedPost->post_date)) ?></span>
                                    </span><!-- MNTime -->
                                    <div class="postShares">
                                        <span class="gPlusShares"></span>
                                        <span><?php echo $meta['gplus'][0]; ?></span>
                                        <span class="twitterShares"></span>
                                        <span><?php echo $meta['twitter'][0]; ?></span>
                                        <span class="facebookShares"></span>
                                        <span><?php echo $meta['facebook'][0]; ?></span>
                                    </div><!-- postShares -->
                                </div><!-- -->
                            </div><!-- rightMNBox -->
                            <?php
                        }
                    }
                    ?>
                </div><!-- rightMN -->

                <div class="rightMN leftMN">
                    <?php
                    $sponsoredID = get_cat_ID('sponsored');
                    $sponsoredPosts = get_posts(array('category__and' => array($sponsoredID, $postCategory->term_id), 'numberposts' => 5));

                    foreach ($sponsoredPosts as $sponsoredPost) {
                        if ($sponsoredPost->ID == $curID) {
                            continue;
                        }
                        $meta = get_post_meta($sponsoredPost->ID);
                        ?>
                        <div class="rightMNBox">
                            <span class="imageMN"><?php echo get_the_post_thumbnail($sponsoredPost->ID, 'thumbnail'); ?></span>
                            <h3><a href="<?php echo get_permalink($sponsoredPost->ID); ?>"><?php echo get_the_title($sponsoredPost->ID) ?></a></h3>
                            <div class="MNBottom">
                                <span class="f-r">
                                    <span class="postTimeIcon"></span>
                                    <span class="postTimeIn"><?php echo date('n/j/Y', strtotime($sponsoredPost->post_date)) ?></span>
                                </span><!-- MNTime -->
                                <div class="postShares">
                                    <span class="gPlusShares"></span>
                                    <span><?php echo $meta['gplus'][0]; ?></span>
                                    <span class="twitterShares"></span>
                                    <span><?php echo $meta['twitter'][0]; ?></span>
                                    <span class="facebookShares"></span>
                                    <span><?php echo $meta['facebook'][0]; ?></span>
                                </div><!-- postShares -->
                            </div><!-- -->
                        </div><!-- rightMNBox -->
                    <?php }
                    ?>
                </div><!-- rightMN -->
            </div><!-- moreNews -->

        </div><!-- inner -->

    </div><!-- rightCol -->

    <div class="sideBar">
        <?php get_sidebar(); ?>
    </div><!-- sideBar -->

</div><!-- container -->

<div id="fb-root"></div>
<?php get_footer(); ?>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery.sharrre-1.3.4.js"></script>
<!-- Place this tag in your head or just before your close body tag. -->
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

<!-- Place this tag where you want the share button to render. -->
<script type="text/javascript">
    $(function() {
        $('#twitter').sharrre({
            share: {
                twitter: true
            },
            enableHover: false,
            enableTracking: true,
            buttons: {twitter: {via: '_JulienH'}},
            click: function(api, options) {
                api.simulateClick();
                api.openPopup('twitter');
            }
        });
        $('#facebook').sharrre({
            share: {
                facebook: true
            },
            enableHover: false,
            enableTracking: true,
            click: function(api, options) {
                api.simulateClick();
                api.openPopup('facebook');
            }
        });


        $('#googleplus').sharrre({
            share: {
                googlePlus: true
            },
            enableHover: false,
            enableTracking: true,
            click: function(api, options) {
                api.simulateClick();
                api.openPopup('googlePlus');
            }
        });

        /* social media */
        (function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id))
                return;
            js = d.createElement(s);
            js.id = id;
            js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    });
</script>